﻿package com.shootingGame.physics
{
	import com.common.dataStructures2.Array2;
	import com.common.physics.CollisionDetection;
	import com.shootingGame.GamePlayManager;
	import com.shootingGame.physics.collisionReactionClasses.base.CollisionEffects;
	import com.shootingGame.renderableClasses.GameObject;
	import com.shootingGame.weaponsClasses.ordinance.Ordinance;
	
	import flash.display.MovieClip;
	import flash.geom.Rectangle;
	
	public class ShooterPhysics
	{
		private static var tileObjects : Array;
		private static var tileData : Array2;

		public function ShooterPhysics()
		{
		}
		
		
		public static function detectObjectToOrdinanceCollision(target:GameObject, ordinance:Ordinance) : Boolean{
			var targetRadius:Number;
			var targetCentreX:Number;
			var targetCentreY:Number;
			var targetCollidablePart:MovieClip;
			var ordinanceCollidablePart:MovieClip;
			var ordinanceCollisionPointX:Number;
			var ordinanceCollisionPointY:Number;
			
			targetCollidablePart = target.getCollidablePart();
			targetCentreX = target.memoryX + target.model.x + targetCollidablePart.x;
			targetCentreY = target.memoryY + target.model.y + targetCollidablePart.y;
			targetRadius = targetCollidablePart.width/2.0;
			ordinanceCollidablePart = ordinance.getCollidablePart();
			ordinanceCollisionPointX = ordinance.memoryX + ordinance.model.x + ordinanceCollidablePart.x;
			ordinanceCollisionPointY = ordinance.memoryY + ordinance.model.y + ordinanceCollidablePart.y;
			if(CollisionDetection.pointCircleIntersection(targetRadius, targetCentreX, targetCentreY, ordinanceCollisionPointX, ordinanceCollisionPointY)){
				
				target.collisionEffects.addCollision(ordinance);
				ordinance.collisionEffects.addCollision(target);
				return true;
			}
			return false; 
		}
		
		public static function detectAndResolve_OrdinanceToTileCollision(gameObject:GameObject) : Boolean{
			return detectAndResolveObjectTileCollisions(gameObject, true);
		}

		public static function detectAndResolve_PlayerToTileCollision(gameObject:GameObject) : Boolean{
			return detectAndResolveObjectTileCollisions(gameObject, false, true);
		}
		
		public static function detectAndResolveObjectTileCollisions(gameObject:GameObject, isOrdinance:Boolean = false, isPlayer:Boolean = false) : Boolean{
			if(!tileObjects){
				
				tileObjects = GamePlayManager.instance.activeMap.tileObjects;
				tileData = GamePlayManager.instance.activeMap.tileData;
			}
			
			var tileObj:GameObject;
			var paramObj:Object;
			var tileWidth:Number = GamePlayManager.TILE_WIDTH;
			var tileHeight:Number = GamePlayManager.TILE_HEIGHT;
			var collidablePart:MovieClip = gameObject.getCollidablePart();
			var radius:Number = collidablePart.width/2.0;
			var centreX:Number = gameObject.memoryX + collidablePart.y;
			var centreY:Number = gameObject.memoryY + collidablePart.x;
			var tileUnits_CoordX:Number = centreX / tileWidth;
			var tileUnits_CoordY:Number = centreY / tileHeight;
			var currentTile_Horizontal_Index : int = Math.floor(tileUnits_CoordX);
			var currentTile_Vertical_Index : int = Math.floor(tileUnits_CoordY);
			
			var collidingTiles:Array = new Array();
			var tileRect:Rectangle = new Rectangle();
			var i : int;
			var j : int;
			var n : int = 3;
			var m : int = 3;
			var startIndex_HorizontalLoop:int;
			var startIndex_VerticalLoop:int;
			if(currentTile_Horizontal_Index <= 0)
				startIndex_HorizontalLoop = 0;
			else
				startIndex_HorizontalLoop = currentTile_Horizontal_Index - 1;
			if(currentTile_Vertical_Index <= 0)
				startIndex_VerticalLoop = 0;
			else
				startIndex_VerticalLoop = currentTile_Vertical_Index - 1;
			for(i = startIndex_HorizontalLoop; i < startIndex_HorizontalLoop + n; i++) {
				for(j = startIndex_VerticalLoop; j < startIndex_VerticalLoop + m; j++) {
//					if(i == currentTile_Horizontal_Index && j == currentTile_Vertical_Index)
//						break;
					tileRect.x = i * tileWidth
					tileRect.y = j * tileHeight
					tileRect.width = tileWidth
					tileRect.height = tileHeight;
					if(isOrdinance){
//						trace(tileWidth, tileHeight);
//						trace(centreX, centreY,	tileRect.left, tileRect.top, tileRect.right, tileRect.bottom);
						if(CollisionDetection.pointRectangleIntersection(centreX, centreY,
						tileRect.left, tileRect.top, tileRect.right, tileRect.bottom)){
							tileObj = tileObjects[tileData.getElement(i, j) as int];
							gameObject.collisionEffects.addCollision(tileObj);
							gameObject.collisionEffects.resolveCollisions();
//							return true;
						}
						continue;
					}
					if(isPlayer){
//						trace(i, j);
						if(i==currentTile_Horizontal_Index && j==currentTile_Vertical_Index)
							continue;
						if(CollisionDetection.circleRectangleIntersection(radius, centreX, centreY,
						tileRect.left, tileRect.top, tileRect.right, tileRect.bottom)){
							tileObj = tileObjects[tileData.getElement(i, j) as int];
							
							paramObj = new Object();
							paramObj[CollisionEffects.PARAM_RECT] = tileRect;
							gameObject.collisionEffects.addCollision(tileObj, paramObj);
							gameObject.collisionEffects.resolveCollisions();
//							return true;
						}
						continue;
					}
						
				}
			}
			return false;
		}
		


	}
}